{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table class=\"ee-notebook-buttons\" align=\"left\">\n",
    "    <td><a target=\"_parent\"  href=\"https://github.com/gee-community/geemap/tree/master/tutorials/ImageCollection/04_mapping_over_image_collection.ipynb\"><img width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /> View source on GitHub</a></td>\n",
    "    <td><a target=\"_parent\"  href=\"https://nbviewer.jupyter.org/github/gee-community/geemap/blob/master/tutorials/ImageCollection/04_mapping_over_image_collection.ipynb\"><img width=26px src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Jupyter_logo.svg/883px-Jupyter_logo.svg.png\" />Notebook Viewer</a></td>\n",
    "    <td><a target=\"_parent\"  href=\"https://colab.research.google.com/github/gee-community/geemap/blob/master/tutorials/ImageCollection/04_mapping_over_image_collection.ipynb\"><img width=26px src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /> Run in Google Colab</a></td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mapping over an ImageCollection\n",
    "To apply a function to every `Image` in an `ImageCollection` use `imageCollection.map()`. The only argument to `map()` is a function which takes one parameter: an `ee.Image`. For example, the following code adds a timestamp band to every image in the collection:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Install Earth Engine API and geemap\n",
    "Install the [Earth Engine Python API](https://developers.google.com/earth-engine/python_install) and [geemap](https://github.com/gee-community/geemap). The **geemap** Python package is built upon the [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [folium](https://github.com/python-visualization/folium) packages and implements several methods for interacting with Earth Engine data layers, such as `Map.addLayer()`, `Map.setCenter()`, and `Map.centerObject()`.\n",
    "The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its [dependencies](https://github.com/gee-community/geemap#dependencies), including earthengine-api, folium, and ipyleaflet.\n",
    "\n",
    "**Important note**: A key difference between folium and ipyleaflet is that ipyleaflet is built upon ipywidgets and allows bidirectional communication between the front-end and the backend enabling the use of the map to capture user input, while folium is meant for displaying static data only ([source](https://blog.jupyter.org/interactive-gis-in-jupyter-with-ipyleaflet-52f9657fa7a)). Note that [Google Colab](https://colab.research.google.com/) currently does not support ipyleaflet ([source](https://github.com/googlecolab/colabtools/issues/60#issuecomment-596225619)). Therefore, if you are using geemap with Google Colab, you should use [`import geemap.foliumap`](https://github.com/gee-community/geemap/blob/master/geemap/foliumap.py). If you are using geemap with [binder](https://mybinder.org/) or a local Jupyter notebook server, you can use [`import geemap`](https://github.com/gee-community/geemap/blob/master/geemap/geemap.py), which provides more functionalities for capturing user input (e.g., mouse-clicking and moving)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Installs geemap package\n",
    "import subprocess\n",
    "\n",
    "try:\n",
    "    import geemap\n",
    "except ImportError:\n",
    "    print(\"geemap package not installed. Installing ...\")\n",
    "    subprocess.check_call([\"python\", \"-m\", \"pip\", \"install\", \"geemap\"])\n",
    "\n",
    "# Checks whether this notebook is running on Google Colab\n",
    "try:\n",
    "    import google.colab\n",
    "    import geemap.foliumap as emap\n",
    "except:\n",
    "    import geemap as emap\n",
    "\n",
    "# Authenticates and initializes Earth Engine\n",
    "import ee\n",
    "\n",
    "try:\n",
    "    ee.Initialize()\n",
    "except Exception as e:\n",
    "    ee.Authenticate()\n",
    "    ee.Initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create an interactive map \n",
    "The default basemap is `Google Satellite`. [Additional basemaps](https://github.com/gee-community/geemap/blob/master/geemap/geemap.py#L13) can be added using the `Map.add_basemap()` function. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "Map.add_basemap(\"ROADMAP\")  # Add Google Map\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Add Earth Engine Python script "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This function adds a band representing the image timestamp.\n",
    "def addTime(image):\n",
    "    return image.addBands(image.metadata(\"system:time_start\"))\n",
    "\n",
    "\n",
    "# Load a Landsat 8 collection for a single path-row.\n",
    "collection = (\n",
    "    ee.ImageCollection(\"LANDSAT/LC08/C01/T1_TOA\")\n",
    "    .filter(ee.Filter.eq(\"WRS_PATH\", 44))\n",
    "    .filter(ee.Filter.eq(\"WRS_ROW\", 34))\n",
    ")\n",
    "\n",
    "first = collection.map(addTime).first()\n",
    "\n",
    "# Map the function over the collection and display the result.\n",
    "# print(collection.map(addTime).getInfo())\n",
    "print(first.bandNames().getInfo())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For one-line function, you can also use a [lambda function](https://www.w3schools.com/python/python_lambda.asp), which is a small anonymous function. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load a Landsat 8 collection for a single path-row.\n",
    "collection = (\n",
    "    ee.ImageCollection(\"LANDSAT/LC08/C01/T1_TOA\")\n",
    "    .filter(ee.Filter.eq(\"WRS_PATH\", 44))\n",
    "    .filter(ee.Filter.eq(\"WRS_ROW\", 34))\n",
    ")\n",
    "\n",
    "first = collection.map(\n",
    "    lambda image: image.addBands(image.metadata(\"system:time_start\"))\n",
    ").first()\n",
    "\n",
    "# Map the function over the collection and display the result.\n",
    "# print(collection.map(addTime).getInfo())\n",
    "print(first.bandNames().getInfo())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that in the predefined function, the `metadata()` method is used to create a new `Image` from the value of a property. As discussed in the [Reducing](https://developers.google.com/earth-engine/ic_reducing) and [Compositing](https://developers.google.com/earth-engine/ic_composite_mosaic) sections, having that time band is useful for the linear modeling of change and for making composites.\n",
    "\n",
    "The mapped function is limited in the operations it can perform. Specifically, it can’t modify variables outside the function; it can’t print anything; it can’t use JavaScript ‘if’ or ‘for’ statements. However, you can use `ee.Algorithms.If()` to perform conditional operations in a mapped function. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This function uses a conditional statement to return the image if\n",
    "# the solar elevation > 40 degrees.  Otherwise it returns a zero image.\n",
    "def conditional(image):\n",
    "    return ee.Algorithms.If(\n",
    "        ee.Number(image.get(\"SUN_ELEVATION\")).gt(40), image, ee.Image(0)\n",
    "    )\n",
    "\n",
    "\n",
    "# Load a Landsat 8 collection for a single path-row.\n",
    "collection = (\n",
    "    ee.ImageCollection(\"LANDSAT/LC08/C01/T1_TOA\")\n",
    "    .filter(ee.Filter.eq(\"WRS_PATH\", 44))\n",
    "    .filter(ee.Filter.eq(\"WRS_ROW\", 34))\n",
    ")\n",
    "\n",
    "# Load a Landsat 8 collection for a single path-row.\n",
    "collection = (\n",
    "    ee.ImageCollection(\"LANDSAT/LC8_L1T_TOA\")\n",
    "    .filter(ee.Filter.eq(\"WRS_PATH\", 44))\n",
    "    .filter(ee.Filter.eq(\"WRS_ROW\", 34))\n",
    ")\n",
    "\n",
    "# Map the function over the collection, convert to a List and print the result.\n",
    "# print('Expand this to see the result: ', collection.map(conditional).getInfo())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Inspect the list of images in the output ImageCollection and note that the when the condition evaluated by the `If()` algorithm is true, the output contains a constant image. Although this demonstrates a server-side conditional function (learn more about client vs. server in Earth Engine on [this page](https://developers.google.com/earth-engine/client_server)), avoid `If()` in general and use filters instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# a function for calculating NDVI for Landsat 8 imagery\n",
    "def calNDVI(image):\n",
    "    return image.normalizedDifference([\"B5\", \"B4\"])\n",
    "\n",
    "\n",
    "point = ee.Geometry.Point(-83.93, 35.85)\n",
    "\n",
    "# select three Landsat 8 imagery in the Knoxville area with the least cloud coverage\n",
    "collection = (\n",
    "    ee.ImageCollection(\"LANDSAT/LC8_L1T_TOA\")\n",
    "    .filterBounds(point)\n",
    "    .sort(\"CLOUD_COVER\")\n",
    "    .limit(3)\n",
    ")\n",
    "\n",
    "# calculate NDVI for each image using the map function\n",
    "ndvi_images = collection.map(calNDVI)\n",
    "\n",
    "vizParams = {\"bands\": [\"B5\", \"B4\", \"B3\"], \"min\": 0, \"max\": 0.4}\n",
    "\n",
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "\n",
    "\n",
    "Map.centerObject(point, 8)\n",
    "\n",
    "for i in range(0, 3):\n",
    "    landsat = ee.Image(collection.toList(3).get(i))\n",
    "    ndvi = ee.Image(ndvi_images.toList(3).get(i))\n",
    "\n",
    "    Map.addLayer(landsat, vizParams, \"Landsat image \" + str(i + 1))\n",
    "    Map.addLayer(ndvi, {\"palette\": [\"red\", \"green\"]}, \"NDVI image \" + str(i + 1))\n",
    "\n",
    "Map.addLayerControl()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use the lambda function "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "point = ee.Geometry.Point(-83.93, 35.85)\n",
    "\n",
    "# select three Landsat 8 imagery in the Knoxville area with the least cloud coverage\n",
    "collection = (\n",
    "    ee.ImageCollection(\"LANDSAT/LC8_L1T_TOA\")\n",
    "    .filterBounds(point)\n",
    "    .sort(\"CLOUD_COVER\")\n",
    "    .limit(3)\n",
    ")\n",
    "\n",
    "# calculate NDVI for each image using the map function\n",
    "ndvi_images = collection.map(lambda image: image.normalizedDifference([\"B5\", \"B4\"]))\n",
    "\n",
    "vizParams = {\"bands\": [\"B5\", \"B4\", \"B3\"], \"min\": 0, \"max\": 0.4}\n",
    "\n",
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "\n",
    "\n",
    "Map.centerObject(point, 8)\n",
    "\n",
    "for i in range(0, 3):\n",
    "    landsat = ee.Image(collection.toList(3).get(i))\n",
    "    ndvi = ee.Image(ndvi_images.toList(3).get(i))\n",
    "\n",
    "    Map.addLayer(landsat, vizParams, \"Landsat image \" + str(i + 1))\n",
    "    Map.addLayer(ndvi, {\"palette\": [\"red\", \"green\"]}, \"NDVI image \" + str(i + 1))\n",
    "\n",
    "Map.addLayerControl()\n",
    "Map"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
